package com.xiyu.service.repository.fee.category;

import com.xiyu.service.model.fee.category.FeeCategory;
import com.xiyu.service.model.fee.category.FeeCategoryTable;
import com.xiyu.service.vo.fee.feeCategory.FeeCategoryQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

public interface FeeCategoryRepository extends JRepository<FeeCategory, Long> {
    FeeCategoryTable feeCategoryTable = FeeCategoryTable.$;

    default Page<FeeCategory> pageQuery(FeeCategoryQueryInput inputVO){
        return sql().createQuery(feeCategoryTable)
                .whereIf(StringUtils.hasText(inputVO.getCategoryName()), () -> feeCategoryTable.categoryName().like(inputVO.getCategoryName()))
                .whereIf(inputVO.getIsFirst() != null, feeCategoryTable.isFirst().eq(inputVO.getIsFirst()))
                .whereIf(inputVO.getIsBuiltin() != null, feeCategoryTable.isBuiltin().eq(inputVO.getIsBuiltin()))
                .orderBy(feeCategoryTable.id().desc())
                .select(feeCategoryTable)
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    Optional<FeeCategory> findFirstByCategoryName(String categoryName);

    Optional<FeeCategory> findFirstByCategoryNameAndIdNot(String categoryName, Long id);

    List<FeeCategory> findByIsFirst(boolean isFirst);

    List<FeeCategory> findByType(Integer type);

    default List<FeeCategory> listQuery(){
        return sql().createQuery(feeCategoryTable)
                .orderBy(feeCategoryTable.id().desc())
                .select(feeCategoryTable).execute();
    }


}